<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/common/common.jsp" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title></title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="">
	<meta http-equiv="description" content="This is my page">
	
	<script type="text/javascript" src="${CONTEXT_PATH}/js/login/business/question/define/questionConstant.js"></script>
  </head>
  
  <body>
    <center>
    	<s:form name="frm" id="frm" method="post" action="" namespace="">
    		<jsp:include page="/jsp/outward/business/common/menuLink.jsp">
    			<jsp:param name="menuLink" value="[{'name':'修改问题'}]"/>
    			<jsp:param name="width" value="50%"/>
    		</jsp:include>
    		
    		<s:hidden name="qes.id" id="qesId"></s:hidden>
    		<s:hidden name="qes.paper.id" id="paperId"></s:hidden>
    		
    		<table cellspacing="1" cellpadding="3" bgcolor="#CCCCCC" width="100%">
    			<tr bgcolor="#FFFFFF">
    				<td width="20%" class="label" align="right">
    					题号：
    				</td>
    				<td width="30%" align="left">
   						<s:textfield name="qes.qesCode" id="qesCode" cssStyle="width:90%;" maxlength="10"></s:textfield>
    				</td>
    				<td width="20%" class="label" align="right">
    					类型：
    				</td>
    				<td width="30%" align="left">
    					<s:select list="qesTypeList" listKey="id" listValue="dataValue" cssStyle="width:100px;" name="qes.qesType.id" id="qesType" onchange="qesTypeChange()"></s:select>
    				</td>
    			</tr>
    			<tr bgcolor="#FFFFFF">
    				<td class="label" align="right">
    					题目：
    				</td>
    				<td align="left" colspan="3">
    					<s:textarea name="qes.qesTitle" id="qesTitle" cssStyle="width:95%; height:50px;"></s:textarea>
    				</td>
    			</tr>
    			<tbody id="eventBody">
    			<tr bgcolor="#FFFFFF">
    				<td class="label" align="right">
    					焦点事件：
    				</td>
    				<td align="left" colspan="3">
    					<s:textarea name="qes.focusEvent" id="focusEvent" cssStyle="width:95%; height:200px;"></s:textarea>
    				</td>
    			</tr>
    			<tr bgcolor="#FFFFFF">
    				<td class="label" align="right">
    					输入事件：
    				</td>
    				<td align="left" colspan="3">
    					<s:textarea name="qes.keyEvent" id="keyEvent" cssStyle="width:95%; height:200px;"></s:textarea>
    				</td>
    			</tr>
    			</tbody>
    			<tr bgcolor="#FFFFFF">
    				<td class="label" align="right">
    					答案宽度：
    				</td>
    				<td align="left">
   						<s:select list="commonList" listKey="value" listValue="label" cssStyle="width:100px;" name="qes.width" id="keyWidth"></s:select>
    				</td>
    				<td class="label" align="right">
    					个数：
    				</td>
    				<td align="left">
    					<s:select list="commonList" listKey="value" listValue="label" cssStyle="width:100px;" name="qes.quantity" id="quantity"></s:select>
    				</td>
    			</tr>
    			<tr bgcolor="#FFFFFF">
    				<td class="label" align="right">
    				<%--
    					第几页：
    				--%>
    				</td>
    				<td align="left">
    				<%--
   						<s:select list="commonList" listKey="value" listValue="label" cssStyle="width:100px;" name="qes.page" id="page"></s:select>
    				--%>
    				</td>
    				<td class="label" align="right">
    					所用码表：
    				</td>
    				<td align="left">
    					<s:select list="ctList" listKey="id" listValue="code" headerKey="" headerValue="--请选择--" cssStyle="width:100px;" name="qes.codeTable.id" id="codeTableId"></s:select>
    					<a href="javascript:updateCodeTable('codeTableId')">修改</a>
    					<a href="javascript:delCodeTable('codeTableId')">删除</a>
    				</td>
    			</tr>
    			<tr bgcolor="#FFFFFF">
    				<td class="label" align="right">
    					组题：
    				</td>
    				<td align="left" colspan="3">
   						<s:select list="subQesList" listKey="id" listValue="code" headerKey="" headerValue="--请选择--" cssStyle="width:100px;" name="qes.groupQes.id" id="groupQes"></s:select>
    					<a href="javascript:updateCodeTable('groupQes')">修改</a>
    					<a href="javascript:delCodeTable('groupQes')">删除</a>
    				</td>
    			</tr>
    		</table>
    		
    		<table width="100%" cellspacing="0">
    			<tr>
    				<td align="center" valign="bottom" height="30">
    					<input type="button" value="新增码表" class="btn" onclick="addCodeTable()" />
    					<input type="button" value="保存" class="btn" onclick="saveThis()" />
    				</td>
    			</tr>
    		</table>
    	</s:form>
    </center>
    
    <script type="text/javascript">
    $(function(){
    	qesTypeChange();
    });
    
    function saveThis()
    {
    	var qesCode = $('#qesCode');
    	var qesTitle = $('#qesTitle');
    	var focusEvent = $('#focusEvent');
    	var keyEvent = $('#keyEvent');
    	var groupQes = $('#groupQes');
    	var qesType = $('#qesType');
    	var codeTableId = $('#codeTableId');
    	if(!isNotNull(qesCode.val()))
    	{
    		showMsg('题号不能为空。', function(){qesCode.focus();});
    		return;
    	}
    	if(qesCode.val().indexOf('_') > -1)
    	{
    		showMsg('题号不能有“_”。', function(){qesCode.focus();});
    		return;
    	}
    	if(!isNotNull(qesTitle.val()))
    	{
    		showMsg('题目不能为空。', function(){qesTitle.focus();});
    		return;
    	}
    	if(qesTitle.val().length > 150)
    	{
    		showMsg('题目不能超过150字。', function(){qesTitle.focus();});
    		return;
    	}
    	if(focusEvent.val().length > 2000)
    	{
    		showMsg('焦点事件不能超过2000字。', function(){focusEvent.focus();});
    		return;
    	}
    	if(keyEvent.val().length > 2000)
    	{
    		showMsg('输入事件不能超过2000字。', function(){keyEvent.focus();});
    		return;
    	}
    	if(GROUP_SINGLE == qesType.val() || GROUP_NUMERIC == qesType.val() || GROUP_MULTIPLE == qesType.val())
    	{
        	if(!isNotNull(groupQes.val()))
        	{
        		showMsg('类型为组时，必须选择组题。', function(){groupQes.focus();});
            	return;
        	}
    	}
    	if(NUMERIC_VALUE != qesType.val() && GROUP_NUMERIC != qesType.val() && FULL_OPEN != qesType.val() && !isNotNull(codeTableId.val()))
    	{
    		showMsg('请选择码表。', function(){codeTableId.focus();});
        	return;
        }
        if(checkKeyEvent() == false)
        {
			return;
        }
        showConfirm('修改逻辑可能会导致问卷答案不准确，需要查错。确认保存？', function(v){
			if(v == true)
			{
				ajaxAction('inner/qes/updateQuestion.action', 'post', 'frm', true, function(result){
		    		if('hasCode' == result)
		    		{
		    			showMsg('已存在该问题编号。', function(){
			   				qesCode.focus();
			   			});
		    		}
		    		else
		    		{
		   				window.opener.searchThis();
		   				window.close();
			   		}
		    	});
			}
        });
    }
    
    function addCodeTable()
    {
    	var url = realPath + '/inner/qes/toAddCodeTable.action?ct.paper.id=' + $('#paperId').val();
    	showMd(url, 700, 600, 'yes', 'yes');
    }
    
    function updateCodeTable(sid)
    {
        var codeTableId = $('#' + sid);
        if(isNotNull(codeTableId.val()))
        {
    		var url = realPath + '/inner/qes/toUpdateCodeTable.action?ct.id=' + codeTableId.val();
    		showMd(url, 700, 600, 'yes', 'yes');
        }
        else
        {
			showMsg('请选择码表。', function(){codeTableId.focus();});
        }
    }
    
    function delCodeTable(sid)
    {
    	var codeTableId = $('#' + sid);
        if(isNotNull(codeTableId.val()))
        {
            showConfirm('确认删除？', function(v){
				if(v == true)
				{
		            var url = 'inner/qes/delCodeTable.action?ct.id=' + codeTableId.val();
		            ajaxAction(url, 'get', 'frm', true, function(obj){
		            	addSelectOption2(codeTableId, obj.ctList, 'id', 'code', true);
		            	codeTableId.val('');
		            });
				}
            });
        }
        else
        {
			showMsg('请选择码表。', function(){codeTableId.focus();});
        }
    }

    function qesTypeChange()
    {
		var qesType = $('#qesType');
		var focusEvent = $('#focusEvent');
		var keyEvent = $('#keyEvent');
		var groupQes = $('#groupQes');
		var keyWidth = $('#keyWidth');
		var quantity = $('#quantity');
		var codeTableId = $('#codeTableId');
		var eventBody = $('#eventBody');
		if(SINGLE_CHOICE == qesType.val())
		{
			eventBody.css('display', 'block');
			keyWidth.attr('disabled', false);
			quantity.attr('disabled', true);
			codeTableId.attr('disabled', false);
			groupQes.attr('disabled', true);
		}
		else if(NUMERIC_VALUE == qesType.val())
		{
			eventBody.css('display', 'block');
			keyWidth.attr('disabled', false);
			quantity.attr('disabled', true);
			codeTableId.attr('disabled', true);
			groupQes.attr('disabled', true);
		}
		else if(MULTIPLE_CHOICE == qesType.val())
		{
			eventBody.css('display', 'block');
			keyWidth.attr('disabled', false);
			quantity.attr('disabled', false);
			codeTableId.attr('disabled', false);
			groupQes.attr('disabled', true);
		}
		else if(FULL_OPEN == qesType.val())
		{
			eventBody.css('display', 'block');
			keyWidth.attr('disabled', false);
			quantity.attr('disabled', false);
			codeTableId.attr('disabled', true);
			groupQes.attr('disabled', true);
		}
		else if(GROUP_SINGLE == qesType.val())
		{
			focusEvent.val('');
			keyEvent.val('');
			eventBody.css('display', 'none');
			keyWidth.attr('disabled', false);
			quantity.attr('disabled', true);
			codeTableId.attr('disabled', false);
			groupQes.attr('disabled', false);
		}
		else if(GROUP_NUMERIC == qesType.val())
		{
			focusEvent.val('');
			keyEvent.val('');
			eventBody.css('display', 'none');
			keyWidth.attr('disabled', false);
			quantity.attr('disabled', true);
			codeTableId.attr('disabled', true);
			groupQes.attr('disabled', false);
		}
		else if(GROUP_MULTIPLE == qesType.val())
		{
			focusEvent.val('');
			keyEvent.val('');
			eventBody.css('display', 'none');
			keyWidth.attr('disabled', false);
			quantity.attr('disabled', false);
			codeTableId.attr('disabled', false);
			groupQes.attr('disabled', false);
		}
    }

    function checkKeyEvent()
    {
        var keyEvent = $('#keyEvent');
        if(keyEvent.val().indexOf('.toEnd') > -1 && keyEvent.val().indexOf('.recover') == -1)
        {
            showMsg('toEnd必须与recover同用。', function(){keyEvent.focus();});
            return false;
        }
        return true;
    }
    </script>
  </body>
</html>
